package com.srxz.personnelsystem.system.controller;

import com.srxz.personnelsystem.system.entity.DepartToPosition;
import com.srxz.personnelsystem.system.mapper.dto.DepartmentDTO;
import com.srxz.personnelsystem.system.service.DepartmentService;
import com.srxz.personnelsystem.util.BackMessage;
import com.srxz.personnelsystem.util.PageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: DepartmentController
 * @author: wgchao
 * @createTime: 2019/1/23 4:53 PM
 * @Description: 部门管理
 */
@RestController
@RequestMapping("/personnel/sys")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 部门新增
     * @param departmentDTO
     * @return
     */
    @PostMapping("/department")
    public BackMessage save(@RequestBody DepartmentDTO departmentDTO){
        if(StringUtils.isEmpty(departmentDTO.getName())){
            return BackMessage.error("部门名称不能为空");
        }
        return departmentService.save(departmentDTO);
    }

    /**
     * 部门更新
     * @param departmentDTO
     * @return
     */
    @PutMapping("/department")
    public BackMessage update(@RequestBody DepartmentDTO departmentDTO){
        if(StringUtils.isEmpty(departmentDTO.getId())){
            return BackMessage.error("主键信息缺失");
        }
        return departmentService.update(departmentDTO);
    }

    /**
     * 部门删除
     * @param departmentDTO
     * @return
     */
    @DeleteMapping("/department")
    public BackMessage delete(@RequestBody DepartmentDTO departmentDTO){
        if(StringUtils.isEmpty(departmentDTO.getId())){
            return BackMessage.error("主键信息缺失");
        }
        return departmentService.delete(departmentDTO);
    }

    /**
     * 部门查找-主键
     * @param id
     * @return
     */
    @GetMapping("/department")
    public BackMessage search(String id){
        if(StringUtils.isEmpty(id)){
            return BackMessage.error("主键信息缺失");
        }
        return departmentService.search(id);
    }

    /**
     * 部门查找-分页
     * @param pageParam
     * @return
     */
    @PostMapping("/departmentofpage")
    public BackMessage searchToPage(@RequestBody PageParam pageParam){
        return departmentService.searchToPage(pageParam);
    }

    @GetMapping("/tree")
    public BackMessage searchAllOfTree(){
        return departmentService.searchAllOfTree();
    }

    @PostMapping("/toposition")
    public BackMessage saveToPosition(@RequestBody DepartToPosition departToPosition){
        if(StringUtils.isEmpty(departToPosition.getDepartmentId())){
            return BackMessage.error("部门信息缺失");
        }
        if(StringUtils.isEmpty(departToPosition.getPositionId())){
            return BackMessage.error("职位信息缺失");
        }
        return departmentService.saveToPosition(departToPosition);
    }

    @DeleteMapping("/toposition")
    public BackMessage deleteToPosition(@RequestBody DepartToPosition departToPosition){
        if(StringUtils.isEmpty(departToPosition.getDepartmentId())){
            return BackMessage.error("部门信息缺失");
        }
        if(StringUtils.isEmpty(departToPosition.getPositionId())){
            return BackMessage.error("职位信息缺失");
        }
        return departmentService.deleteToPosition(departToPosition);
    }

    @DeleteMapping("/topositionlist")
    public BackMessage deleteToPositionlist(@RequestBody List<DepartToPosition> departToPositions){

        return departmentService.deleteToPositionlist(departToPositions);
    }

    @GetMapping("canuse")
    public BackMessage searchAllCanUse(){
        return departmentService.searchAllCanUse();
    }

}
